<?php
class TreatmentRecordsController extends AppController {

	var $name = 'TreatmentRecords';
	
	public function beforeFilter() {
            parent::beforeFilter();
            //$this->Auth->allow('index');
        }
	
	function index()
	{
		Controller::loadModel('TreatmentCategory');
		$users = $this->TreatmentRecord->User->find('list');
		$this->layout = "home";
		$city_id = $this->Session->read("c_city_id");
		$year = $this->Session->read("c_year");
		$quarter = $this->Session->read("c_quarter");
		
		//Listing branches
		$branches = $this->TreatmentRecord->Branch->find('all',array(								  
									'fields' => array('Branch.id', 'Branch.name', 'Branch.city_id'),
									'conditions' => array(
												'Branch.city_id'=>$city_id,
												'Branch.type'=>2,
											),
								));
		//Listing treatments
		$treatment_categories = $this->TreatmentCategory->find("all");
		//echo count($treatment_categories);
		$treatment_catid = 2;
		$treatment_conditions = array();
		if($treatment_catid)
		{
			$treatment_conditions["treatment_category_id"] = $treatment_catid;
			
		}
		$treatments = $this->TreatmentRecord->Treatment->find('all',array(
								  
									'fields' => array('Treatment.id', 'Treatment.name'),
									'conditions' => $treatment_conditions,
									'order'=>'Treatment.ordering'
									
								));		
		
		$notEmpty["out"] = 0;
		
		//create values array for rendering view
		foreach($treatments as $e => $treatment)
		{
			$total_t = 0;
			foreach($branches as $b => $branch)
			{
				$treatmentresource = $this->TreatmentRecord->find("first", array(
											'conditions' => array(
												'TreatmentRecord.branch_id' => $branch["Branch"]["id"],
												'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
												'TreatmentRecord.year' => $year,
												'TreatmentRecord.quarter' => $quarter
											)
										));
				
				if($treatmentresource["TreatmentRecord"]["value"] != "") $notEmpty["out"]++;
				
				$values[$branch["Branch"]["id"]][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["value"];
				$total_t += $treatmentresource["TreatmentRecord"]["value"];
			}
			$treatments[$e]['total'] = $total_t;
		}
		
		//noi tru
		$treatment_catid = 1;
		$treatment_conditions = array();
		if($treatment_catid)
		{
			$treatment_conditions["treatment_category_id"] = $treatment_catid;
		}
		$treatments_in = $this->TreatmentRecord->Treatment->find('all',array(
								  
									'fields' => array('Treatment.id', 'Treatment.name'),
									'conditions' => $treatment_conditions,
									'order'=>'Treatment.ordering'
									
								));
		
		$notEmpty["in"] = 0;
		//create values array for rendering view
		foreach($treatments_in as $e => $treatment)
		{
			
			//noi tru
			$treatmentresource_in = $this->TreatmentRecord->find("first", array(
										'conditions' => array(
											'TreatmentRecord.city_id' => $city_id,
											'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
											'TreatmentRecord.year' => $year,
											'TreatmentRecord.quarter' => $quarter
										)
									));
			
			if($treatmentresource["TreatmentRecord"]["total"] != "") $notEmpty["in"]++;
			
			$values_in['total'][$treatment["Treatment"]["id"]] = $treatmentresource_in["TreatmentRecord"]["total"];
			$values_in['note'][$treatment["Treatment"]["id"]] = $treatmentresource_in["TreatmentRecord"]["note"];
		}
		
		//listing unit
		$this->data["TreatmentRecord"]["unit_id"] = !empty($this->data["TreatmentRecord"]["unit_id"])?$this->data["TreatmentRecord"]["unit_id"]:3;
		
		$this->set(compact('treatment_catid', 'treatment_categories', 'cities', 'branches', 'treatments', 'treatments_in', 'values','values_in', 'year', 'quarter', 'notEmpty'));
	}

	function manager_index() {
		$this->TreatmentRecord->recursive = 0;
		Controller::loadModel('Branch');
		
		//get cities list
		Controller::loadModel('City');
		$user = $this->Auth->user();
		$cities = $this->City->citiesByUser($user["User"]["id"]);
		//var_dump($cities);
		$conditions = array();
		
		$c_year = $this->Session->read("c_year");
		
		//filter city id
		$city_id = !empty($this->params['form']['filter_city_id']) && $this->params['form']['filter_city_id'] != "" ? $this->params['form']['filter_city_id'] : $cities[0]["City"]["id"];
		if($city_id)
		{
			$conditions["Branch.city_id"] = $city_id;
		}
		
		//filter branch
		$branches = $this->Branch->find("all", array('conditions' => array('Branch.city_id'=>$city_id)));
		$branch_id = !empty($this->params['form']['filter_branch_id']) && $this->params['form']['filter_branch_id'] != "" ? $this->params['form']['filter_branch_id'] : 0;
		if($branch_id)
		{
			$conditions["Branch.id"] = $branch_id;
		}
		
		//filter year
		$year = !empty($this->params['form']['filter_year']) && $this->params['form']['filter_year'] != "" ? $this->params['form']['filter_year'] : $c_year;
		if($year)
		{
			$conditions["TreatmentRecord.year"] = $year;
		}
		
		
		//var_dump($this->params);
		if(count($conditions))
		{
			$this->paginate = array(
				'conditions' => $conditions				
			);
		}
		
		$this->set('treatmentRecords', $this->paginate());
		$this->set('cities', $cities);
		$this->set('branches', $branches);
		$this->set('city_id', $city_id);
		$this->set('branch_id', $branch_id);
		$this->set('year', $year);
	}
	
	function manager_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid treatment record', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('treatmentRecord', $this->TreatmentRecord->read(null, $id));
	}
	
	function admin_add() {
		if(!$this->Session->read("isActive"))
		{
			$this->redirect(array('controller'=>'users' , 'action' => 'offline', 'manager'=>false));
		}
		Controller::loadModel('City');
		Controller::loadModel('User');
		Controller::loadModel('TreatmentCategory');
		$users = $this->TreatmentRecord->User->find('list');
		//$city = $this->City->find("first");
		$c_year = $this->Session->read("c_year");
		$c_quarter = $this->Session->read("c_quarter");
		
		
		//Check if user have role on city
		$user = $this->Auth->user();
		$user = $this->TreatmentRecord->User->find("first", array(
									'conditions'=>array('User.id'=>$user["User"]["id"])									
								));		
		
		//find city_id
		$cities = $user["City"];
		if(!empty($this->params['form']['filter_city_id']))
		{
			foreach($cities as $c)
			{
				if($c["id"] == $this->params['form']['filter_city_id'])
				{
					$city = $c;
					break;
				}
			}
		}
		else if(!empty($this->data["TreatmentRecord"]["city_id"]))
		{
			$city = $this->City->find("first", array(
									'conditions'=>array('City.id'=>$this->data["TreatmentRecord"]["city_id"])									
								));
			$city = $city["City"];
		}
		else
		{
			$city = $user["City"][0];
		}
		
		
		//Listing branches
		$branches = $this->TreatmentRecord->Branch->find('all',array(								  
									'fields' => array('Branch.id', 'Branch.name', 'Branch.city_id'),
									'conditions' => array(
												'Branch.city_id'=>$city["id"],
												'Branch.type'=>2,
											),
								));
		//Listing treatments
		$treatment_categories = $this->TreatmentCategory->find("all");
		//echo count($treatment_categories);
		$treatment_catid = !empty($this->params['form']['filter_treatment_catid']) && $this->params['form']['filter_treatment_catid'] != "" ? $this->params['form']['filter_treatment_catid'] : 1;
		$treatment_conditions = array();
		if($treatment_catid)
		{
			$treatment_conditions["treatment_category_id"] = $treatment_catid;
		}
		$treatments = $this->TreatmentRecord->Treatment->find('all',array(
								  
									'fields' => array('Treatment.id', 'Treatment.name'),
									'conditions' => $treatment_conditions,
									'order'=>'Treatment.ordering'
									
								));		
		
		$year = !empty($this->params['form']['filter_year']) ? $this->params['form']['filter_year'] : $c_year;
		$quarter = !empty($this->params['form']['filter_quarter']) ? $this->params['form']['filter_quarter'] : $c_quarter;
		//$year = date('Y');
		//$quarter = date('m')%3 + 1;
		
		$user = $this->Auth->user();
		
		//echo $user["User"]["id"];
		
		
		//check if the user has accessibility to city					
		if(!$this->User->isUserCity($user["User"]["id"], $city["id"]))
		{
			$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
			$this->redirect(array('action' => 'index'));
		}
			
			
		
		if (!empty($this->data)) {			
			
			if($treatment_catid == 2)
			{
				//Saving TreatmentRecords values
				foreach($treatments as $e => $treatment)
				{
					foreach($branches as $b => $branch)
					{
						$value = $this->data["values"][$branch["Branch"]["id"]][$treatment["Treatment"]["id"]];
						
						//Convert to good-looking number
						$value = str_replace(",", "", $value);
						
						//check if record exsits
						$exsit = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.branch_id' => $branch["Branch"]["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $this->data["TreatmentRecord"]["year"],
													'TreatmentRecord.quarter' => $this->data["TreatmentRecord"]["quarter"]
												)
											));
							
						//echo $exsit["TreatmentRecord"]["id"];
						$treatmentResource = $this->data;
						$treatmentResource["TreatmentRecord"]["value"] = $value;
						$treatmentResource["TreatmentRecord"]["city_id"] = $branch["Branch"]["city_id"];
						$treatmentResource["TreatmentRecord"]["branch_id"] = $branch["Branch"]["id"];
						$treatmentResource["TreatmentRecord"]["treatment_id"] = $treatment["Treatment"]["id"];
						$treatmentResource["TreatmentRecord"]["user_id"] = $user["User"]["id"];
						
						//echo $treatmentResource["TreatmentRecord"]["value"];					
							
						
						if(empty($exsit["TreatmentRecord"]["id"]))
						{
							if($value != "") //if there first record, create record and store the value
							{						
								$this->TreatmentRecord->create();
								if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
								} else {
									$this->Session->setFlash(__('The human resource could not be saved. Please, try again.', true));
								}
							}					
						}
						else //if record exsits, change its value
						{
							$treatmentResource["TreatmentRecord"]["id"] = $exsit["TreatmentRecord"]["id"];
							//echo $value."o";
							if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
							} else {
								$this->Session->setFlash(__('The treatment resource could not be saved. Please, try again.', true));
							}
						}					
					}
				}
				$year = $this->data["TreatmentRecord"]["year"];
				$quarter = $this->data["TreatmentRecord"]["quarter"];
			}
			else
			{
				//Saving TreatmentRecords values
				foreach($treatments as $e => $treatment)
				{
					
						$total = $this->data["total"][$treatment["Treatment"]["id"]];
						$note = $this->data["note"][$treatment["Treatment"]["id"]];
						
						//Convert to good-looking number
						$total = str_replace(",", "", $total);
						
						//check if record exsits
						$exsit = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.city_id' => $city["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $this->data["TreatmentRecord"]["year"],
													'TreatmentRecord.quarter' => $this->data["TreatmentRecord"]["quarter"]
												)
											));
							
						//echo $exsit["TreatmentRecord"]["id"];
						$treatmentResource = $this->data;
						$treatmentResource["TreatmentRecord"]["total"] = $total;
						$treatmentResource["TreatmentRecord"]["note"] = $note;
						$treatmentResource["TreatmentRecord"]["city_id"] = $city["id"];
						$treatmentResource["TreatmentRecord"]["branch_id"] = 0;
						$treatmentResource["TreatmentRecord"]["treatment_id"] = $treatment["Treatment"]["id"];
						$treatmentResource["TreatmentRecord"]["user_id"] = $user["User"]["id"];
						
						//echo $treatmentResource["TreatmentRecord"]["value"];					
							
						
						if(empty($exsit["TreatmentRecord"]["id"]))
						{
							if($total != "") //if there first record, create record and store the value
							{						
								$this->TreatmentRecord->create();
								if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
								} else {
									$this->Session->setFlash(__('The human resource could not be saved. Please, try again.', true));
								}
							}					
						}
						else //if record exsits, change its value
						{
							$treatmentResource["TreatmentRecord"]["id"] = $exsit["TreatmentRecord"]["id"];
							//echo $value."o";
							if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
							} else {
								$this->Session->setFlash(__('The treatment resource could not be saved. Please, try again.', true));
							}
						}					
					
				}
				$year = $this->data["TreatmentRecord"]["year"];
				$quarter = $this->data["TreatmentRecord"]["quarter"];
			}
			
		}
		
		if($treatment_catid == 2)
		{
			//create values array for rendering view
			foreach($treatments as $e => $treatment)
			{
				foreach($branches as $b => $branch)
				{
					$treatmentresource = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.branch_id' => $branch["Branch"]["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $year,
													'TreatmentRecord.quarter' => $quarter
												)
											));
					$values[$branch["Branch"]["id"]][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["value"];
					
				}
			}
		}
		else
		{
			//create values array for rendering view
			foreach($treatments as $e => $treatment)
			{				
					$treatmentresource = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.city_id' => $city["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $year,
													'TreatmentRecord.quarter' => $quarter
												)
											));
					$values['total'][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["total"];
					$values['note'][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["note"];

			}
		}
		
		//listing unit
		$units = $this->TreatmentRecord->Unit->find('list');
		$this->data["TreatmentRecord"]["unit_id"] = !empty($this->data["TreatmentRecord"]["unit_id"])?$this->data["TreatmentRecord"]["unit_id"]:3;
		
		$this->set(compact('treatment_catid', 'treatment_categories', 'users', 'cities', 'branches', 'treatments', 'values', 'year', 'quarter', 'quarter', 'city', 'units'));
	}

	function manager_add() {
		if(!$this->Session->read("isActive"))
		{
			$this->redirect(array('controller'=>'users' , 'action' => 'offline', 'manager'=>false));
		}
		Controller::loadModel('City');
		Controller::loadModel('User');
		Controller::loadModel('TreatmentCategory');
		$users = $this->TreatmentRecord->User->find('list');
		//$city = $this->City->find("first");
		$c_year = $this->Session->read("c_year");
		$c_quarter = $this->Session->read("c_quarter");
		
		
		//Check if user have role on city
		$user = $this->Auth->user();
		$user = $this->TreatmentRecord->User->find("first", array(
									'conditions'=>array('User.id'=>$user["User"]["id"])									
								));		
		
		//find city_id
		$cities = $user["City"];
		if(!empty($this->params['form']['filter_city_id']))
		{
			foreach($cities as $c)
			{
				if($c["id"] == $this->params['form']['filter_city_id'])
				{
					$city = $c;
					break;
				}
			}
		}
		else if(!empty($this->data["TreatmentRecord"]["city_id"]))
		{
			$city = $this->City->find("first", array(
									'conditions'=>array('City.id'=>$this->data["TreatmentRecord"]["city_id"])									
								));
			$city = $city["City"];
		}
		else
		{
			$city = $user["City"][0];
		}
		
		
		//Listing branches
		$branches = $this->TreatmentRecord->Branch->find('all',array(								  
									'fields' => array('Branch.id', 'Branch.name', 'Branch.city_id'),
									'conditions' => array(
												'Branch.city_id'=>$city["id"],
												'Branch.type'=>2,
											),
								));
		//Listing treatments
		$treatment_categories = $this->TreatmentCategory->find("all");
		//echo count($treatment_categories);
		$treatment_catid = !empty($this->params['form']['filter_treatment_catid']) && $this->params['form']['filter_treatment_catid'] != "" ? $this->params['form']['filter_treatment_catid'] : 1;
		$treatment_conditions = array();
		if($treatment_catid)
		{
			$treatment_conditions["treatment_category_id"] = $treatment_catid;
		}
		$treatments = $this->TreatmentRecord->Treatment->find('all',array(
								  
									'fields' => array('Treatment.id', 'Treatment.name'),
									'conditions' => $treatment_conditions,
									'order'=>'Treatment.ordering'
									
								));		
		
		//$year = !empty($this->params['form']['filter_year']) ? $this->params['form']['filter_year'] : $c_year;
		//$quarter = !empty($this->params['form']['filter_quarter']) ? $this->params['form']['filter_quarter'] : $c_quarter;
		$year = date('Y');
		$quarter = round(date('m')/3,0);
		
		$user = $this->Auth->user();
		
		//echo $user["User"]["id"];
		
		
		//check if the user has accessibility to city					
		if(!$this->User->isUserCity($user["User"]["id"], $city["id"]))
		{
			$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
			$this->redirect(array('action' => 'index'));
		}
			
			
		
		if (!empty($this->data)) {			
			
			if($treatment_catid == 2)
			{
				//Saving TreatmentRecords values
				foreach($treatments as $e => $treatment)
				{
					foreach($branches as $b => $branch)
					{
						$value = $this->data["values"][$branch["Branch"]["id"]][$treatment["Treatment"]["id"]];
						
						//Convert to good-looking number
						$value = str_replace(",", "", $value);
						
						//check if record exsits
						$exsit = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.branch_id' => $branch["Branch"]["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $this->data["TreatmentRecord"]["year"],
													'TreatmentRecord.quarter' => $this->data["TreatmentRecord"]["quarter"]
												)
											));
							
						//echo $exsit["TreatmentRecord"]["id"];
						$treatmentResource = $this->data;
						$treatmentResource["TreatmentRecord"]["value"] = $value;
						$treatmentResource["TreatmentRecord"]["city_id"] = $branch["Branch"]["city_id"];
						$treatmentResource["TreatmentRecord"]["branch_id"] = $branch["Branch"]["id"];
						$treatmentResource["TreatmentRecord"]["treatment_id"] = $treatment["Treatment"]["id"];
						$treatmentResource["TreatmentRecord"]["user_id"] = $user["User"]["id"];
						
						//echo $treatmentResource["TreatmentRecord"]["value"];					
							
						
						if(empty($exsit["TreatmentRecord"]["id"]))
						{
							if($value != "") //if there first record, create record and store the value
							{						
								$this->TreatmentRecord->create();
								if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
								} else {
									$this->Session->setFlash(__('The human resource could not be saved. Please, try again.', true));
								}
							}					
						}
						else //if record exsits, change its value
						{
							$treatmentResource["TreatmentRecord"]["id"] = $exsit["TreatmentRecord"]["id"];
							//echo $value."o";
							if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
							} else {
								$this->Session->setFlash(__('The treatment resource could not be saved. Please, try again.', true));
							}
						}					
					}
				}
				$year = $this->data["TreatmentRecord"]["year"];
				$quarter = $this->data["TreatmentRecord"]["quarter"];
			}
			else
			{
				//Saving TreatmentRecords values
				foreach($treatments as $e => $treatment)
				{
					
						$total = $this->data["total"][$treatment["Treatment"]["id"]];
						$note = $this->data["note"][$treatment["Treatment"]["id"]];
						
						//Convert to good-looking number
						$total = str_replace(",", "", $total);
						
						//check if record exsits
						$exsit = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.city_id' => $city["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $this->data["TreatmentRecord"]["year"],
													'TreatmentRecord.quarter' => $this->data["TreatmentRecord"]["quarter"]
												)
											));
							
						//echo $exsit["TreatmentRecord"]["id"];
						$treatmentResource = $this->data;
						$treatmentResource["TreatmentRecord"]["total"] = $total;
						$treatmentResource["TreatmentRecord"]["note"] = $note;
						$treatmentResource["TreatmentRecord"]["city_id"] = $city["id"];
						$treatmentResource["TreatmentRecord"]["branch_id"] = 0;
						$treatmentResource["TreatmentRecord"]["treatment_id"] = $treatment["Treatment"]["id"];
						$treatmentResource["TreatmentRecord"]["user_id"] = $user["User"]["id"];
						
						//echo $treatmentResource["TreatmentRecord"]["value"];					
							
						
						if(empty($exsit["TreatmentRecord"]["id"]))
						{
							if($total != "") //if there first record, create record and store the value
							{						
								$this->TreatmentRecord->create();
								if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
								} else {
									$this->Session->setFlash(__('The human resource could not be saved. Please, try again.', true));
								}
							}					
						}
						else //if record exsits, change its value
						{
							$treatmentResource["TreatmentRecord"]["id"] = $exsit["TreatmentRecord"]["id"];
							//echo $value."o";
							if ($this->TreatmentRecord->save($treatmentResource)) {
								$this->Session->setFlash(__('Thông tin đã được lưu', true));
								//$this->redirect(array('action' => 'index'));							
								//break;
							} else {
								$this->Session->setFlash(__('The treatment resource could not be saved. Please, try again.', true));
							}
						}					
					
				}
				$year = $this->data["TreatmentRecord"]["year"];
				$quarter = $this->data["TreatmentRecord"]["quarter"];
			}
			
		}
		
		if($treatment_catid == 2)
		{
			//create values array for rendering view
			foreach($treatments as $e => $treatment)
			{
				foreach($branches as $b => $branch)
				{
					$treatmentresource = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.branch_id' => $branch["Branch"]["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $year,
													'TreatmentRecord.quarter' => $quarter
												)
											));
					$values[$branch["Branch"]["id"]][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["value"];
					
				}
			}
		}
		else
		{
			//create values array for rendering view
			foreach($treatments as $e => $treatment)
			{				
					$treatmentresource = $this->TreatmentRecord->find("first", array(
												'conditions' => array(
													'TreatmentRecord.city_id' => $city["id"],
													'TreatmentRecord.treatment_id' => $treatment["Treatment"]["id"],
													'TreatmentRecord.year' => $year,
													'TreatmentRecord.quarter' => $quarter
												)
											));
					$values['total'][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["total"];
					$values['note'][$treatment["Treatment"]["id"]] = $treatmentresource["TreatmentRecord"]["note"];

			}
		}
		
		//listing unit
		$units = $this->TreatmentRecord->Unit->find('list');
		$this->data["TreatmentRecord"]["unit_id"] = !empty($this->data["TreatmentRecord"]["unit_id"])?$this->data["TreatmentRecord"]["unit_id"]:3;
		
		$this->set(compact('treatment_catid', 'treatment_categories', 'users', 'cities', 'branches', 'treatments', 'values', 'year', 'quarter', 'quarter', 'city', 'units'));
	}

	function manager_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid treatment record', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			
			//check if the user has accessibility to city
			Controller::loadModel('Branch');
			$user = $this->Auth->user();
			$branch = $this->Branch->read(null, $this->data["TreatmentRecord"]["branch_id"]);
			Controller::loadModel('User');
			if(!$this->User->isUserCity($user["User"]["id"], $branch["Branch"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
			
			if ($this->TreatmentRecord->save($this->data)) {
				$this->Session->setFlash(__('The treatment record has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The treatment record could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->TreatmentRecord->read(null, $id);
			$year = $this->data["TreatmentRecord"]["year"];
			
			Controller::loadModel('Branch');
			$user = $this->Auth->user();
			$branch = $this->Branch->read(null, $this->data["TreatmentRecord"]["branch_id"]);
			
		}
		$users = $this->TreatmentRecord->User->find('list');
		$branches = $this->TreatmentRecord->Branch->find('list', array('conditions'=>array('Branch.city_id'=>$branch["Branch"]["city_id"])));
		$treatments = $this->TreatmentRecord->Treatment->find('list');
		$units = $this->TreatmentRecord->Unit->find('list');
		$this->set(compact('users', 'branches', 'treatments', 'units', 'year'));
	}

	function manager_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for treatment record', true));
			$this->redirect(array('action'=>'index'));
		}
		
		//check if user has role on city
			$this->data = $this->TreatmentRecord->read(null, $id);
			//check if the user has accessibility to city
			Controller::loadModel('Branch');
			$user = $this->Auth->user();
			$branch = $this->Branch->read(null, $this->data["TreatmentRecord"]["branch_id"]);
			Controller::loadModel('User');
			if(!$this->User->isUserCity($user["User"]["id"], $branch["Branch"]["city_id"]))
			{
				$this->Session->setFlash(__('Manager doesn\'t has role on this city', true));
				$this->redirect(array('action' => 'index'));
			}
		
		if ($this->TreatmentRecord->delete($id)) {
			$this->Session->setFlash(__('Treatment record deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Treatment record was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
